var Calendar = {
  config: {
    obj: null,
    from_date: '',
    to_date: '',
    after_draw: function () {
    },
  },
  init: function (obj, after_draw) {
    var _this = this;
    _this.config.obj = obj;
    _this.after_draw = after_draw;

    _this.config.cur_date = date('Y-m-01');
    _this.draw_body();
    _this.init_btn();

  },
  init_btn: function () {
    var _this = this;

    //上个月
    $(document).on('click', '#calendar_last_month_btn', function (e) {
      e.preventDefault();
      var cur_date = _this.config.cur_date;
      _this.config.cur_date = date('Y-m-d', strtotime('-1 month', strtotime(cur_date)));
      _this.draw_body();

    });

    //下个月
    $(document).on('click', '#calendar_next_month_btn', function (e) {
      e.preventDefault();
      var cur_date = _this.config.cur_date;
      _this.config.cur_date = date('Y-m-d', strtotime('+1 month', strtotime(cur_date)));
      _this.draw_body();
    });

    //今天
    $(document).on('click', '#calendar_today_btn', function (e) {
      e.preventDefault();
      var cur_date = _this.config.cur_date;
      _this.config.cur_date = date('Y-m-d');
      _this.draw_body();
    });
  },
  draw_body: function () {
    var _this = this;
    var inner_html = '';

    var cur_month_time = strtotime(_this.config.cur_date);
    var cur_title = date('Y-m', cur_month_time);
    var cur_month = date('m', cur_month_time);
    var today = date('Y-m-d');

    //本月第一天
    var this_month_first_day = date('Y-m-', cur_month_time) + '01';
    var this_month_last_day = date('Y-m-', cur_month_time) + date('t');



    var w = date('w', strtotime(this_month_first_day));

    var start_date = date('Y-m-d', strtotime('-' + w + ' days', strtotime(this_month_first_day)))
    var len = 42;
    for (var i = 0; i < len; i++) {
      var t = strtotime('+' + i + ' days', strtotime(start_date));
      var day = date('Y-m-d', t);
      var m = date('m', t);
      var d = date('d', t);
      if (i % 7 == 0) {
        inner_html += '<tr>';
      }
      if (i > 1 && i % 7 == 0) {
        inner_html += '</tr>';
      }

      var this_month_class = m == cur_month ? 'this-month' : '';
      inner_html += '<td class="cell" data-day="' + day + '"><div class="day ' + this_month_class + '">' + d + '</div></td>';
    }
    _this.config.from_date = start_date;
    _this.config.from_date = date('Y-m-d', strtotime('+42 days', strtotime(start_date)));

    inner_html += '</tr>';
    var html = '<h4 class="calendar-title">' + cur_title + '</h4><div class="calendar-button"><div class="btn-group" role="group" style="margin-right:16px;">';
    html += '<button type="button" id="calendar_last_month_btn" class="btn btn-default"><i class="fa fa-arrow-left"></i></button>';
    html += '<button type="button" id="calendar_next_month_btn" class="btn btn-default"><i class="fa fa-arrow-right"></i></button>';
    html += '</div><button class="btn btn-default" id="calendar_today_btn" ><i class="fa fa-home"></i> 今天 ' + today + '</button></div>';
    html += '<div class="table-responsive"><table class="table table-bordered">';
    html += '<thead><tr><th>周日</th><th>周一</th><th>周二</th><th>周三</th><th>周四</th><th>周五</th><th>周六</th></tr></thead>';
    html += '<tbody>' + inner_html + '</tbody></table></div>';
    _this.config.obj.html(html);

    $('td[data-day="' + today + '"]').addClass('today');
    _this.after_draw(_this.config.cur_date);
  },
  add_tips: function (data) {
    for (var i = 0; i < data.length; i++) {
      if (data[i]['status'] == 1) {
        $('td[data-day="' + data[i]['date'] + '"]').append('<div class="unauth"></div>');
      } else {
        $('td[data-day="' + data[i]['date'] + '"]').append('<div class="auth"></div>');
      }

    }
  },
}